package com.yh.workbill.ui.count.mvp;

import android.content.SharedPreferences;

import com.yh.workbill.App;
import com.yh.workbill.AppGlobals;
import com.yh.workbill.base.IBaseModel;
import com.yh.workbill.db.UserInfo;
import com.yh.workbill.db.UserInfoDao;
import com.yh.workbill.model.Count;
import com.yh.workbill.network.BaseCallback;
import com.yh.workbill.network.OkHttpService;

import java.util.List;

import javax.security.auth.callback.Callback;

import androidx.arch.core.executor.ArchTaskExecutor;

import static com.yh.workbill.utils.Config.SP_USER;

public class Count_Model {

    interface CountDataCallBack extends IBaseModel.Fail {
        void onSuccess(List<Count> data);
    }

    public void getCountDatas(int pageindex, int pagenumber, CountDataCallBack callBack) {
        ArchTaskExecutor.getIOThreadExecutor().execute(() -> {
            SharedPreferences sp = AppGlobals.getApplication().getSharedPreferences(SP_USER, AppGlobals.getApplication().MODE_PRIVATE);
            String phone = sp.getString("phone", "");
            UserInfoDao userInfoDao = App.appDataBase.getUserInfoDao();
            UserInfo singleUserInfo = userInfoDao.getSingleUserInfo(phone);
            String user_uuid = singleUserInfo.getUuid();
            OkHttpService.getInstance().getCounts(user_uuid, pageindex, pagenumber, new BaseCallback<List<Count>>() {
                @Override
                public void accessSuccess(int code, String msg, List<Count> data) {
                    ArchTaskExecutor.getMainThreadExecutor().execute(() -> {
                        if (code == 200) {
                            callBack.onSuccess(data);
                        } else {
                            callBack.fail(msg);
                        }
                    });
                }

                @Override
                public void accessFailure(String errorMessage) {
                    callBack.fail(errorMessage);
                }
            });
        });
    }
}
